<?php

namespace App\Logic;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Cache;
use Xrjyz\Curl\Curl;

class Sms
{

    public static function smsSend($phone,$content){

        /*
            短信接口

            uid   8834
            pw   md5(990439.'tm')
            mb  手机号
            ms  内容
            tm  时间格式  14位
            dm  定时时间选填
            ex  发送扩展号，对于可扩展的通道，可以根据需要任意扩展，但发送出去总长度不会超过20位。

         * */
        $sms_config = [

            'uid' => 8834,
            'password' => 990439,
            'sms_url' => 'http://sms.jpsw666.com:18002/send.do',
            'balance_url' => 'http://sms.jpsw666.com:18005/balance.do',

        ];

        $dated = date('YmdHis',time());
        $password = md5($sms_config['password'].$dated);

        $curl = new Curl;
        $curl->post('uid',$sms_config['uid'])
            ->post('pw',$password)
            ->post('mb',$phone)
            ->post('ms',$content)
            ->post('tm',$dated)
            ->url($sms_config['sms_url']);

        $response = [
            -1  => 'uid或密码为空',
            -2  => '号码为空',
            -3  => '内容为空',
            -4  => '内容超长',
            -5  => '下发号码超长',
            -6  => 'uid或密码不正确',
            -7  => '余额不足',
            -8  => 'ip鉴权未通过',
            -9  => '协议类型不对',
            -99 => '系统异常',
        ];

        if ($curl->error()) {
            return e_msg(300009,$curl->message());
        }

        if($curl->info()['http_code'] == 200){

            $content = $curl->data();
            if(!in_array($content,$response)){
                return s_msg('发送成功');
            }else{
                return e_msg(300010,$response[$content]);
            }
        }else{
            return e_msg(300009,'响应异常'.$curl->info()['http_code']);
        }






    }

    public static function balance(){


        /*

            uid   8834
            pw   md5(990439.'tm')
            mb  手机号
            ms  内容
            tm  时间格式  14位
            dm  定时时间选填
            ex  发送扩展号，对于可扩展的通道，可以根据需要任意扩展，但发送出去总长度不会超过20位。

         * */
        $sms_config = [

            'uid' => 8834,
            'password' => 990439,
            'sms_url' => 'http://sms.jpsw666.com:18002/send.do',
            'balance_url' => 'http://sms.jpsw666.com:18005/balance.do',

        ];

        $date = date('YmdHis',time());
        $password = $sms_config['password'].$date;

        $curl = new Curl;

        $balance_url = $sms_config['balance_url'].'?uid='.$sms_config['uid'].'&pw='.md5($sms_config['password'].$date).'&tm='.$date;

        $curl->url($balance_url);





        //http://sms.jpsw666.com:18002/send.do?uid=8834&pw=md5(密码+tm)&mb=13900000001&ms=测试【测试】&tm=20191207151746
        dd($curl);
        if ($curl->error() ) {

            return e_msg($curl->message());
        } else {
            if($curl->info()['http_code'] == 200){
                $content = $curl->data();
                Cache::set('file_coin',$content);
                Log::info('执行时间  '.$time.'  执行成功');
                echo "success";
            }else{
                Log::info('执行时间  '.$time.'  执行失败'.$curl->info()['http_code']);
                echo 'error'.$curl->info()['http_code'];
            }

        }




    }


}
